require 'Enums.rb'

#
# CPL Assignment 2
# Tim Caljé, Jeroen De Raedt & Jorick Van Aken
#
class FlightsInterpreter
  
  def initialize(connectionDao)
    @connectionDao =  connectionDao
    @destinations = Array.new
    @dates = Array.new
    @amountSeats = 0
  end
  
  def from(from)
    if @from == nil
      @from = from
    else
      raise Exception, "You can only use 1 from in the query!"
    end
    self
  end
  
  def to(to)
    @destinations.push(to)
    self
  end
  
  def on(date)
    @dates.push(date)
    self
  end
  
  def filterBusiness()
    if @filter == nil
      @filter = SeatType::BUSINESS
    else
      raise Exception, "You can only use 1 filter in the query!"
    end
    self
  end
  
  def filterEconomy()
    if @filter == nil
      @filter = SeatType::ECONOMY
    else
      raise Exception, "You can only use 1 filter in the query!"
    end
    self
  end

  def filterFirstClass()
    if @filter == nil
      @filter = SeatType::FIRST_CLASS
    else
      raise Exception, "You can only use 1 filter in the query!"
    end
    self
  end
  
  def sortCheapestFirst()
    if @order == nil
      @order = FlightsOrder::CHEAPEST
    else
      raise Exception, "You can only specify 1 order in the query!"
    end
    self
  end
  
  def sortFastestFirst()
    if @order == nil
      @order = FlightsOrder::SHORTEST
    else
      raise Exception, "You can only specify 1 order in the query!"
    end
    self
  end
  
  def seats(amount)
    if @amountSeats == 0
      @amountSeats = amount
    else
      raise Exception, "You can only specify the number of seats once in the query!"
    end
    self    
  end
  
  def query
    routes = @connectionDao.getFlights(@from, @destinations, @dates, @filter, @amountSeats, @order)
    returnString = ""
    for counter in 0...routes.length
      returnString += "Day #{counter + 1} (#{@dates[counter]}): #{routes[counter].length} routes found:\n"
      routes[counter].each do |route|
        returnString += route.to_s + "\n"
      end
    end
    return returnString
  end
  
end